<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表格Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>修改文章</legend>
    </fieldset>


    <form class="layui-form" action="" lay-filter="example" method="POST" action="" style="padding-right: 20px;">
        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">文章编号:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" name="rticleaId" disabled="true" autocomplete="off" placeholder="" id="rticleaId"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">文章标题</label>
            <div class="layui-input-block">
                <input type="text" name="rticleaTitle" id="rticleaTitle" autocomplete="off" id="name"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">文章时间</label>
            <div class="layui-input-block">
                <input type="text" name="rticleaTime" id="rticleaTime" autocomplete="off" id="name" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block">
                <textarea name="rticleaComm" id="rticleaComm" required lay-verify="required" placeholder="请输入"
                    class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="uploadBtn">选择图片</button>
            <div class="layui-upload-list" id="previewBox"></div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" id="submit" lay-submit="">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <style>
        .layui-upload {
            margin-left: 108px;
        }

        .layui-upload-list {
            padding: 10px 40px;
            display: flex;
            min-height: 150px;
            min-width: 150px;
            border: 1px solid #cccccc;
        }

        .imgitem {
            cursor: pointer;
            background-color: #FF5722;
            width: 100%;
            text-align: center;
        }

        .imgBox {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin: 0px 20px;
        }
    </style>
    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>
    <script src="../js/util.js" charset="utf-8"></script>
    <script>

        layui.config({
            base: '/module/'
        }).use(['jquery', 'layer', 'table', 'form', 'config', 'laydate', 'upload'], function () {

            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var config = layui.config;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var imgUrl = ''
            var files = [];
            var srcArr = new Array();
            var req = GetRequest();

            var no = req['editNo'];

            $.ajax({
                url: config.base_server + 'getRticleaById?editNo=' + no,
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    data = data.data;
                    $('#rticleaId').val(data.rticleaId);
                    $('#rticleaTitle').val(data.rticleaTitle);
                    laydate.render({
                        elem: '#rticleaTime',
                        value: data.rticleaTime
                        , type: 'datetime'
                        , trigger: 'click'
                    });
                    $('#rticleaComm').val(data.rticleaComm);
                    srcArr = data.rticleaImgList;
                    srcArr.forEach((item, index) => {
                        $('#previewBox').append('<div class="imgBox" ><img src="' + item + '" alt="" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>');
                        $('.remove_' + index).bind('click', function () {
                            let itemIndex = $(this).parent().parent().find('.imgBox').index($(this).parent()[0]);
                            srcArr.splice(itemIndex, 1);
                            $(this).parent().remove();
                        })
                    })
                }
            })

            upload.render({
                elem: '#uploadBtn'
                , multiple: true
                , bindAction: "#save"
                , auto: false
                , choose: (obj) => {//预览图片
                    files = obj.pushFile();
                    obj.preview((index, file, result) => {
                        imgUrl = result
                        $('#previewBox').append('<div class="imgBox" ><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>')
                        $('.remove_' + index).bind('click', function () {
                            delete files[index];//删除指定图片
                            $(this).parent().remove();
                        })
                    });
                }
            });

            $('#submit').on('click', function () {
                var data = form.val('example');
                var rticleaTitle = data.rticleaTitle;
                var rticleaId = data.rticleaId;
                var rticleaTime = data.rticleaTime;
                var rticleaComm = data.rticleaComm;
                var formData = new FormData();
                formData.append('rticleaTitle', rticleaTitle);
                formData.append('rticleaTime', rticleaTime);
                formData.append('rticleaComm', rticleaComm);
                formData.append('rticleaId', rticleaId);
                for (var i = 0; i < Object.values(files).length; i++) {
                    formData.append('rticleaImg', Object.values(files)[i]);
                }
                formData.append("rticleaImgList", JSON.stringify(srcArr));
                $.ajax({
                    type: "POST",
                    url: config.base_server + "updataRticlea",
                    async: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            alert("修改成功");
                            parent.layer.close(index); //再执行关闭   
                        } else {
                            alert(data.msg);
                        }
                    },
                    error() {
                        alert("修改失败")
                    }
                });
                return false;
            });

        });
    </script>


</body>